#include "changepassword.h"
#include "ui_changepassword.h"

extern QString password;

changepassword::changepassword(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::changepassword)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_DeleteOnClose);

    QFile file(":/settings.qss"); // 假设style.qss位于资源文件中
    if (file.open(QFile::ReadOnly))
    {
        setStyleSheet(QString(file.readAll())); // 应用全局样式
        file.close();
    }

    setWindowFlag(Qt::FramelessWindowHint,true);//去掉主窗口的边框
    setAttribute(Qt::WA_TranslucentBackground,true);//把主窗口透明

    connect(this,SIGNAL(changePassword(QString)),this->parent(),SLOT(changePassword(QString)));
}

changepassword::~changepassword()
{
    qDebug()<<"changepassword";
    delete ui;
}

void changepassword::on_closeButton_clicked()
{
    this->close();
}

void changepassword::mousePressEvent(QMouseEvent *event)
{
    if(false == ui->sureButton->underMouse() && false == ui->closeButton->underMouse())
        mouseOff = event->globalPos() - geometry().topLeft();
}

void changepassword::mouseMoveEvent(QMouseEvent *event)
{
    if(false == ui->sureButton->underMouse() && false == ui->closeButton->underMouse())
        this->move(event->globalPos() - mouseOff);
}

void changepassword::on_passwordLineEdit_textChanged(const QString &arg1)
{
    if(password != arg1)
    {
        QToolTip::showText(ui->passwordLineEdit->mapToGlobal(QPoint(25, 15)), "密码输入错误，请重新输入密码！");
    }
}


void changepassword::on_reNewPasswordLineEdit_textChanged(const QString &arg1)
{
    if(arg1 != ui->NewpasswordLineEdit->text())
    {
        QToolTip::showText(ui->reNewPasswordLineEdit->mapToGlobal(QPoint(25, 15)), "两次密码输入不一致，请重新进行输入！");
    }
}


void changepassword::on_NewpasswordLineEdit_textChanged(const QString &arg1)
{
    if(true == arg1.contains(' '))
    {
        QToolTip::showText(ui->NewpasswordLineEdit->mapToGlobal(QPoint(25, 15)), "密码不允许包含空格，请重新进行输入！");
    }
    else
    {
        if(password == arg1)
        {
            QToolTip::showText(ui->NewpasswordLineEdit->mapToGlobal(QPoint(25, 15)), "新密码与旧密码一致，请重新进行输入！");
        }
    }
}


void changepassword::on_sureButton_clicked()
{
    if(true == ui->NewpasswordLineEdit->text().contains(' '))
    {
        QToolTip::showText(ui->NewpasswordLineEdit->mapToGlobal(QPoint(25, 15)), "密码不允许包含空格，请重新进行输入！");
    }
    else
    {
        if(password == ui->NewpasswordLineEdit->text())
        {
            QToolTip::showText(ui->NewpasswordLineEdit->mapToGlobal(QPoint(25, 15)), "新密码与旧密码一致，请重新进行输入！");
        }
        else
        {
            if(ui->reNewPasswordLineEdit->text() == ui->NewpasswordLineEdit->text())
            {
                emit changePassword(ui->NewpasswordLineEdit->text());
            }
            else
            {
                QToolTip::showText(ui->reNewPasswordLineEdit->mapToGlobal(QPoint(25, 15)), "两次密码输入不一致，请重新进行输入！");
            }
        }
    }
}

